<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <!-- 左侧 -->
          <el-tab-pane label="角色管理" name="roles">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="showDialog=true"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table v-loading="loading" :data="list" :border="true" stripe>
              <el-table-column type="index" label="序号" :index="indexMethod" width="100" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="showEdit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="del(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
              :current-page="page"
              :page-sizes="[1, 2, 3, 4]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="company">
            <!-- 公司信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 角色新增/修改 -->
      <el-dialog :visible="showDialog" :title="title" @click="closeDialog">
        <!-- 表单区域 -->
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item prop="name" label="角色名称">
            <el-input v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item prop="description" label="角色描述">
            <el-input v-model="form.description" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button size="small">取消</el-button>
          <el-button size="small" type="primary" @click="submitBtn">确定</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { reqGetRoleList, reqDeleteRole, reqAddRole, reqGetRoleDetail, reqUpdateRole } from '@/api/setting'
import { reqGetCompanyById } from '@/api/company'
import { mapState } from 'vuex'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'roles',
      list: [], // 列表数据
      page: 1, // 页码
      pagesize: 2, // 每页条数
      total: 0, // 总条数
      loading: false,
      showDialog: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请填写角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请填写角色描述', trigger: 'blur' }
        ]
      },
      // 公司信息
      companyInfo: {}
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑角色' : '添加角色'
    },
    ...mapState('user', ['userInfo'])
  },
  created() {
    this.getRolesList()
  },
  methods: {

    // 获取角色列表
    async getRolesList() {
      this.loading = true
      const { data: { rows, total }} = await reqGetRoleList(this.page, this.pagesize)
      console.log(rows)
      this.list = rows
      this.total = total
      console.log(this.total)
      this.loading = false
    },
    // 每页条数发送变化触发
    handleSizeChange(newSize) {
      this.pagesize = newSize
      // this.pagesize = 1
      this.getRolesList()
    },
    // 页码发生变化触发
    handleCurrentChange(newPage) {
      this.page = newPage
      this.getRolesList()
    },
    indexMethod(index) {
      return index + 1 + (this.page - 1) * this.pagesize
    },
    // 删除
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {

      }).then(async() => {
        const { message } = await reqDeleteRole(id)
        // console.log(message)
        this.$message.success(message)
        this.getRolesList()
      }).catch(() => {
        this.$message.info('已取消操作')({
        })
      })
    },
    closeDialog() {
      this.showDialog = false
      // 重置表单
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    // 添加
    submitBtn() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        if (this.form.id) {
          // 有id 编辑
          const { message } = await reqUpdateRole(this.form)
          this.$message.success(message)
        } else {
          // 没id 新建
          const { message } = await reqAddRole(this.form)
          // console.log(res)
          this.$message.success(message)
        }
        this.showDialog = false
        this.getRolesList()
      })
    },
    // 更新
    async showEdit(id) {
      const res = await reqGetRoleDetail(id)
      console.log(res)
      this.showDialog = true
      this.form = res.data
    },
    handleClick(tab) {
      // console.log(tab)
      if (tab.name === 'company') {
        this.getcompanyInfo()
      }
    },
    async getcompanyInfo() {
      // console.log(1111111111)
      const res = await reqGetCompanyById(this.userInfo.companyId)
      console.log(res)
      this.companyInfo = res.data
    }
  }
}
</script>
